#include "SDLSprite.h"

SDLSprite::SDLSprite()
	: SDLComponent()
	, srcRect(), dstRect()
	, texture(nullptr)
	, visible(true)
{
}

SDLSprite::SDLSprite(std::string fileName)
	: SDLComponent()
	, srcRect(), dstRect()
	, texture(nullptr)
	, visible(true)
{
	SetTexture(fileName);
}

SDLSprite::SDLSprite(std::string fileName, int dstX, int dstY)
	: SDLComponent()
	, srcRect(), dstRect()
	, texture(nullptr)
	, visible(true)
{
	SetSrcRect(0, 0, dstX, dstY);
	SetTexture(fileName);
}

SDLSprite::SDLSprite(std::string fileName, int srcX, int srcY, int srcW, int srcH, int dstX, int dstY)
	: SDLComponent()
	, srcRect(), dstRect()
	, texture(nullptr)
	, visible(true)
{
	SetSrcRect(srcX, srcY, srcW, srcH);
	SetDstRect(dstX, dstY, 0, 0);
	SetOffPoint(0, 0);
	SetTexture(fileName);
}

SDLSprite::SDLSprite(std::string fileName, SDL_Rect& src, int dstX, int dstY)
	: SDLComponent()
	, srcRect(src), dstRect()
	, texture(nullptr)
	, visible(true)
{
	dstRect.x = dstX;
	dstRect.y = dstY;
	SetTexture(fileName);
}

SDLSprite::SDLSprite(std::string fileName, int srcX, int srcY, int srcW, int srcH, SDL_Rect& dst)
	: SDLComponent()
	, srcRect(), dstRect(dst)
	, texture(nullptr)
	, visible(true)
{
	SetSrcRect(srcX, srcY, srcW, srcH);
	SetTexture(fileName);
}

SDLSprite::SDLSprite(std::string fileName, SDL_Rect& src, SDL_Rect& dst)
	: SDLComponent()
	, srcRect(src), dstRect(dst)
	, texture(nullptr)
	, visible(true)
{
	SetTexture(fileName);
}

SDLSprite::~SDLSprite()
{
}

void SDLSprite::Start()
{
}

void SDLSprite::Stop()
{
}

void SDLSprite::Update()
{
}

void SDLSprite::ScaleBy(float multiplicator)
{
	float wW = (float)srcRect.w * multiplicator;
	float hH = (float)srcRect.h * multiplicator;

	dstRect.x = (int)(centerPoint.x - wW / 2.0f);
	dstRect.y = (int)(centerPoint.y - hH / 2.0f);

	dstRect.w = wW;
	dstRect.h = hH;
}

void SDLSprite::Draw()
{
	if(visible)
	{
		if(texture != nullptr)
		{
			ApplySurface(texture, SDLRenderer::GetInstance()->GetRenderer());
		}
	}
}

void SDLSprite::ApplySurface(SDL_Texture* texture, SDL_Renderer* renderer)
{
	SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
}

